package com.zw.yzk.kabu.view.widget;

import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.data.utils.SharedPrefrenceUtils;
import com.zw.yzk.kabu.management.AppManager;

/**
 * Created by zhanwei on 2017/5/23.
 */

public class UpdateDialog {
    private Context context;
    private AlertDialog dialog;
    private TextView content;
    private View progressLayout;
    private TextView progressText;
    private ProgressBar progress;
    private CheckBox ignore;
    private Button download;
    private Button cancel;
    private int versionCode;
    private boolean forceUpdate;

    public UpdateDialog(Context context, int versionCode, boolean forceUpdate) {
        this.context = context;
        this.versionCode = versionCode;
        this.forceUpdate = forceUpdate;
        View rootView = LayoutInflater.from(context).inflate(R.layout.dialog_show_update, null);
        dialog = new AlertDialog
                .Builder(context)
                .setView(rootView)
                .setCancelable(false)
                .create();
        initView(rootView);
    }

    public void show() {
        int ignoreVersionCode = SharedPrefrenceUtils.getInt(context, SharedPrefrenceUtils.getUpdateKey());
        if (!forceUpdate && ignoreVersionCode == versionCode) {
            return;
        }
        dialog.show();
    }

    public void dismiss() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    public boolean isShowing() {
        return dialog != null && dialog.isShowing();
    }


    public void setStartListener(final View.OnClickListener downloadListener) {
        download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressLayout.setVisibility(View.VISIBLE);
                ignore.setVisibility(View.GONE);
                download.setEnabled(false);
                cancel.setEnabled(false);
                if (downloadListener != null) {
                    downloadListener.onClick(v);
                }
            }
        });
    }

    public void updateProgress(int p) {
        progress.setProgress(p);
        progressText.setText(context.getResources().getString(R.string.download_progress, p));
    }

    public void setContent(String content) {
        this.content.setText(content);
    }

    private void initView(View parent) {
        content = (TextView) parent.findViewById(R.id.update_content);
        progressLayout = parent.findViewById(R.id.layout_progress);
        progressText = (TextView) parent.findViewById(R.id.progress_text);
        progress = (ProgressBar) parent.findViewById(R.id.progress);
        ignore = (CheckBox) parent.findViewById(R.id.ignore_the_version);
        download = (Button) parent.findViewById(R.id.download_the_version);
        cancel = (Button) parent.findViewById(R.id.ignore_the_update);

        ignore.setVisibility(forceUpdate ? View.GONE : View.VISIBLE);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (forceUpdate) {
                    AppManager.logout(context);
                } else if (ignore.isChecked()) {
                    SharedPrefrenceUtils.setValue(context, SharedPrefrenceUtils.getUpdateKey(), versionCode);
                }
                dismiss();
            }
        });
    }
}
